package com.xyz.code;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.xyz.code.builder.impl.DtoBuilder;
import com.xyz.code.builder.impl.EntityBuilder;
import com.xyz.code.builder.impl.FacadeImplementBuilder;
import com.xyz.code.builder.impl.FacadeInterfaceBuilder;
import com.xyz.code.builder.impl.RepositoryImplementBuilder;
import com.xyz.code.builder.impl.RepositoryInterfaceBuilder;
import com.xyz.code.builder.impl.ServiceImplementBuilder;
import com.xyz.code.builder.impl.ServiceInterfaceBuilder;
import com.xyz.code.builder.impl.VoBuilder;
import com.xyz.code.util.MyFileUtil;
import com.xyz.code.util.StringUtils;

public class App extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField outFileNameTextField;
	private JButton createButton;
	private JFileChooser chooser;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JCheckBox entityCheckBox;
	private JCheckBox repoCheckBox;
	private JCheckBox repoImplCheckBox;
	private JButton chooseFileButton;
	private JCheckBox serviceCheckBox;
	private JCheckBox serviceImplCheckBox;
	private JCheckBox facadeCheckBox;
	private JCheckBox facadeImplCheckBox;
	//private JCheckBox boCheckBox;
	private JCheckBox voCheckBox;
	private JCheckBox dtoCheckBox;
	private JTextArea inputTableNametextArea;
	private JScrollPane scrollPane;
	private JTextField entityPackageTextField;
	private JTextField voPackageTextField;
	private JTextField irepPackageTextField;
	private JTextField repimplPackageTextField;
	private JTextField iserPackageTextField;
	private JTextField serimplPackageTextField;
	private JTextField ifacadePackageTextField;
	private JTextField facadeImplPackageTextField;
	private JTextField modelTextField;
	private JTextField txtComffcscrmdomaindto;
	
	public static void main(String[] args) {
		App frame = new App();
		frame.setVisible(true);
	}
	
	public void chooseFile(ActionEvent event){
		if (event.getSource().equals(chooseFileButton)) {// 判断触发方法的按钮是哪个  
   		 chooser.setFileSelectionMode(1);// 设定只能选择到文件夹  
            int state = chooser.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句  
            if (state == 1) {  
                return;  
            } else {  
                File f = chooser.getSelectedFile();// f为选择到的目录  
                outFileNameTextField.setText(f.getAbsolutePath());  
            }  
        }  
	}
	
	public String getPackageName(String packageContext, String type){
		String beginPackage = "";
		String endPackage = "";
		int index = packageContext.indexOf("${", 0);
		int end = packageContext.indexOf("}", index);
		if(StringUtils.isEmpty(map.get(type+"beginPackage"))&&index !=-1){
			beginPackage = packageContext.substring(0,index).trim();
			map.put(type+"beginPackage", beginPackage);
		}else{
			beginPackage = map.get(type+"beginPackage");
		}
		if(StringUtils.isEmpty(map.get(type+"endPackage"))&&index !=-1){
			endPackage = packageContext.substring(end+1,packageContext.length()).trim();
			map.put(type+"endPackage", endPackage);
		}else{
			endPackage = map.get(type+"endPackage");
		}
		String newPackageName = "";
		if(StringUtils.isEmpty(modelTextField.getText())){
			newPackageName = beginPackage + "${模块名}" + endPackage;
		}else{
			newPackageName = beginPackage + modelTextField.getText() + endPackage;
		}
		map.put(type+"PackagePath", newPackageName);
		return newPackageName;
	}
	
	Map<String, String> map = new HashMap<String, String>();
	
	
	public void changeAllPackageName(DocumentEvent e){
		entityPackageTextField.setText(getPackageName(entityPackageTextField.getText(),"entity"));
		voPackageTextField.setText(getPackageName(voPackageTextField.getText(),"vo"));
		txtComffcscrmdomaindto.setText(getPackageName(txtComffcscrmdomaindto.getText(),"dto"));
		irepPackageTextField.setText(getPackageName(irepPackageTextField.getText(),"irep"));
		repimplPackageTextField.setText(getPackageName(repimplPackageTextField.getText(),"repimpl"));
		iserPackageTextField.setText(getPackageName(iserPackageTextField.getText(),"iser"));
		serimplPackageTextField.setText(getPackageName(serimplPackageTextField.getText(),"serimpl"));
		ifacadePackageTextField.setText(getPackageName(ifacadePackageTextField.getText(),"ifacade"));
		facadeImplPackageTextField.setText(getPackageName(facadeImplPackageTextField.getText(),"facadeimpl"));
	}
	
	class TextFieldValueChanged implements DocumentListener  {
		@Override
		public void insertUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
			changeAllPackageName(e);
		}
		@Override
		public void removeUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
			changeAllPackageName(e);
		}
		@Override
		public void changedUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
		}
	} 
	
	public void create(ActionEvent event){
		if(!StringUtils.isNotEmpty(outFileNameTextField.getText())){
			JOptionPane.showMessageDialog(null, "请选择生成文件的路径！", "提示", 2);  
			return;
		}
		String outFileName = outFileNameTextField.getText().replaceAll("\\\\", "/")+"/";
		if(!StringUtils.isNotEmpty(inputTableNametextArea.getText())){
			JOptionPane.showMessageDialog(null, "请输入表名，多个表请用‘，’隔开！", "提示", 2);  
			return;
		}
		
		File file = new File(outFileName+"out/");
		MyFileUtil.deleteDir(file);
		String packagePath = "";
		String[] tableNames = null;
		if(inputTableNametextArea.getText().indexOf(",")!=-1){
			tableNames = inputTableNametextArea.getText().split(",");
		}else{
			tableNames = inputTableNametextArea.getText().split("，");
		}
		try {
			for (String tableName : tableNames) {
				//if(boCheckBox.isSelected())
				//	new BOBuilder(tableName, basePackage,outFileName).builder();
				if(voCheckBox.isSelected()){
					packagePath = voPackageTextField.getText();
					new VoBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(dtoCheckBox.isSelected()){
					packagePath = txtComffcscrmdomaindto.getText();
					new DtoBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(facadeCheckBox.isSelected()){
					packagePath = ifacadePackageTextField.getText();
					new FacadeInterfaceBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(facadeImplCheckBox.isSelected()){
					packagePath = facadeImplPackageTextField.getText();
					new FacadeImplementBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(entityCheckBox.isSelected()){
					packagePath = entityPackageTextField.getText();
					new EntityBuilder(tableName, packagePath,outFileName,map).builder();
				}
				//new MapperClassBuilder(tableName, basePackage).builder();
				//new MapperXmlBuilder(tableName, basePackage).builder();
				if(repoCheckBox.isSelected()){
					packagePath = irepPackageTextField.getText();
					new RepositoryInterfaceBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(repoImplCheckBox.isSelected()){
					packagePath = repimplPackageTextField.getText();
					new RepositoryImplementBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(serviceCheckBox.isSelected()){
					packagePath = iserPackageTextField.getText();
					new ServiceInterfaceBuilder(tableName, packagePath,outFileName,map).builder();
				}
				if(serviceImplCheckBox.isSelected()){
					packagePath = serimplPackageTextField.getText();
					new ServiceImplementBuilder(tableName, packagePath,outFileName,map).builder();
				}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "发生错误，生成文件失败，请重新生成", "提示", 2);  
		}
		JOptionPane.showMessageDialog(null, "生成结束！", "提示", 2);  
	}
	
	public App() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 580, 567);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		this.setResizable(false);
		createButton = new JButton("生成");
		createButton.setBounds(249, 506, 66, 23);
		label = new JLabel("选择生成文件目录：");
		label.setBounds(69, 18, 108, 15);
	    chooser = new JFileChooser();
		label_1 = new JLabel("输入需要生成的表名：");
		label_1.setBounds(69, 83, 120, 15);
		label_2 = new JLabel("选择需要生成的类：");
		label_2.setBounds(69, 216, 120, 15);
		entityCheckBox = new JCheckBox("entity类");
		entityCheckBox.setBounds(69, 237, 85, 23);
		repoCheckBox = new JCheckBox("repository接口类");
		repoCheckBox.setBounds(69, 318, 138, 23);
		repoImplCheckBox = new JCheckBox("repository实现类");
		repoImplCheckBox.setBounds(69, 343, 138, 23);
		outFileNameTextField = new JTextField();
		outFileNameTextField.setBounds(69, 43, 343, 21);
		outFileNameTextField.setEnabled(false);
		outFileNameTextField.setColumns(10);
		chooseFileButton = new JButton("选择...");
		chooseFileButton.setBounds(422, 42, 75, 23);
		serviceCheckBox = new JCheckBox("service接口类");
		serviceCheckBox.setBounds(69, 368, 120, 23);
		serviceImplCheckBox = new JCheckBox("service实现类");
		serviceImplCheckBox.setBounds(69, 393, 120, 23);
		facadeCheckBox = new JCheckBox("facade接口类");
		facadeCheckBox.setBounds(69, 418, 120, 23);
		facadeImplCheckBox = new JCheckBox("facade实现类");
		facadeImplCheckBox.setBounds(69, 443, 108, 23);
		//boCheckBox = new JCheckBox("BO类");
		voCheckBox = new JCheckBox("vo类");
		voCheckBox.setBounds(69, 265, 60, 23);
		inputTableNametextArea = new JTextArea();
		inputTableNametextArea.setLineWrap(true);
		inputTableNametextArea.setToolTipText("若输入多个表，请用“，”隔开！");
		scrollPane = new JScrollPane(inputTableNametextArea);
		scrollPane.setBounds(69, 108, 428, 92);
		scrollPane.setVerticalScrollBarPolicy( 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		contentPane.setLayout(null);
		contentPane.add(label_1);
		contentPane.add(label_3);
		contentPane.add(label_2);
		contentPane.add(entityCheckBox);
		contentPane.add(voCheckBox);
		contentPane.add(scrollPane);
		contentPane.add(outFileNameTextField);
		contentPane.add(chooseFileButton);
		contentPane.add(serviceCheckBox);
		contentPane.add(repoCheckBox);
		contentPane.add(facadeCheckBox);
		contentPane.add(facadeImplCheckBox);
		contentPane.add(serviceImplCheckBox);
		contentPane.add(repoImplCheckBox);
		contentPane.add(label);
		contentPane.add(createButton);
		entityPackageTextField = new JTextField();
		entityPackageTextField.setText("com.xyz.${模块名}.entity");
		entityPackageTextField.setBounds(213, 241, 284, 21);
		contentPane.add(entityPackageTextField);
		entityPackageTextField.setColumns(10);
		voPackageTextField = new JTextField();
		voPackageTextField.setText("com.xyz.${模块名}.api.vo");
		voPackageTextField.setBounds(213, 267, 284, 21);
		contentPane.add(voPackageTextField);
		voPackageTextField.setColumns(10);
		irepPackageTextField = new JTextField();
		irepPackageTextField.setText("com.xyz.${模块名}.repository");
		irepPackageTextField.setBounds(213, 320, 284, 21);
		contentPane.add(irepPackageTextField);
		irepPackageTextField.setColumns(10);
		repimplPackageTextField = new JTextField();
		repimplPackageTextField.setText("com.xyz.${模块名}.repository.impl");
		repimplPackageTextField.setBounds(213, 345, 284, 21);
		contentPane.add(repimplPackageTextField);
		repimplPackageTextField.setColumns(10);
		iserPackageTextField = new JTextField();
		iserPackageTextField.setText("com.xyz.${模块名}.service");
		iserPackageTextField.setBounds(213, 370, 284, 21);
		contentPane.add(iserPackageTextField);
		iserPackageTextField.setColumns(10);
		serimplPackageTextField = new JTextField();
		serimplPackageTextField.setText("com.xyz.${模块名}.service.impl");
		serimplPackageTextField.setBounds(213, 395, 284, 21);
		contentPane.add(serimplPackageTextField);
		serimplPackageTextField.setColumns(10);
		ifacadePackageTextField = new JTextField();
		ifacadePackageTextField.setText("com.xyz.${模块名}.api.facade");
		ifacadePackageTextField.setBounds(213, 420, 284, 21);
		contentPane.add(ifacadePackageTextField);
		ifacadePackageTextField.setColumns(10);
		facadeImplPackageTextField = new JTextField();
		facadeImplPackageTextField.setText("com.xyz.${模块名}.facade.impl");
		facadeImplPackageTextField.setBounds(213, 445, 284, 21);
		contentPane.add(facadeImplPackageTextField);
		facadeImplPackageTextField.setColumns(10);
		modelTextField = new JTextField();
		modelTextField.setBounds(431, 210, 66, 21);
		contentPane.add(modelTextField);
		modelTextField.setColumns(10);
		
	      label_3 = new JLabel("输入包名：");
	      label_3.setBounds(213, 216, 96, 15);
		JLabel label_4 = new JLabel("模块名：");
		label_4.setBounds(380, 216, 54, 15);
		contentPane.add(label_4);
		
		dtoCheckBox = new JCheckBox("dto类");
		dtoCheckBox.setBounds(69, 293, 138, 23);
		contentPane.add(dtoCheckBox);
		
		txtComffcscrmdomaindto = new JTextField();
		txtComffcscrmdomaindto.setText("com.xyz.${模块名}.api.dto");
		txtComffcscrmdomaindto.setColumns(10);
		txtComffcscrmdomaindto.setBounds(213, 294, 284, 21);
		contentPane.add(txtComffcscrmdomaindto);
		modelTextField.getDocument().addDocumentListener(new TextFieldValueChanged()); 
		chooseFileButton.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent event) {  
            	chooseFile(event);  
            }  
        });  
		createButton.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent event) {  
            	create(event);  
            }  
        });  
	}
}
